package com.zyh;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author zyh
 * @Date 2024/8/8 19:18
 * @Description
 */

public class Test {
    public static void main(String[] args) {
        /*int[] arr = {};
        int[] arr1 = {};
        int i = addedInteger(arr, arr1);
        System.out.println("i = " + i);*/

        int[] ints = {3, 2, 3};
        int[] ints1 = twoSum(ints, 6);
        System.out.println(Arrays.toString(ints1));
    }

    public static int[] twoSum(int[] nums, int target) {
        //查表法
        Map<Integer,Integer> map=new HashMap<>(nums.length-1);
        map.put(nums[0],0);
        for(int i=1;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }
            map.put(nums[i],i);
        }
        return null;

        //暴力枚举
        /*int index1=-1,index2=-1;
        for(int i=0;i<nums.length-1;i++){
            for(int j=i;j<nums.length-1-i;j++){
                if(nums[i]+nums[j]==target){
                    index1=j;
                    index2=j+1;
                }
            }
        }
        int[] arr={index1,index2};
        return arr;*/
    }
    public static int addedInteger(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        if (nums1.length > 0 && nums2.length > 0) {
            int num = nums2[0] - nums1[0];
            boolean flag = true;
            for (int i = 0; i < nums2.length; i++) {
                if (nums2[i] - nums1[i] != num) {
                    flag = false;
                }
            }
            if (flag) {
                return num;
            }
        }
        return -1;
    }
}

